package com.chee.plant.logic.user;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import org.json.JSONException;
import org.json.JSONObject;
import com.chee.plant.hibernate.dao.UserFriendDAO;
import com.chee.plant.hibernate.object.User;
import com.chee.plant.logic.CmdHandler;
import com.chee.plant.logic.PlantCmd;
import com.chee.plant.server.Message;

/**
 * 获取用户好友列表
 * 
 * @author xue cb
 * 
 */
public class UserFriendListHandler implements CmdHandler {
	@Override
	public Vector<Message> executeCmd(Message msg) {
		// 解析客户端传过来的内容
		Vector<Message> _msgs = new Vector<Message>();
		int userId = msg.getUserId();
		// 进行数据库操作
		List<User> list = new UserFriendDAO().getUserFriendByOwnerId(userId);
		int result = 1;
		Message ret = new Message(userId, PlantCmd.GetUserFriendList, result);
		ArrayList<JSONObject> friendList = new ArrayList<JSONObject>();
		Iterator<User> iter = list.iterator();
		while (iter.hasNext()) {
			User user = iter.next();
			JSONObject obj = new JSONObject();
			try {
				obj.append("id", user.getId());
				obj.append("name", user.getName());
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				// 添加json格式错误处理
				e.printStackTrace();
			}
			// ....
			friendList.add(obj);
		}
		ret.addVariable("list", friendList);
		_msgs.add(ret);
		return _msgs;
	}

	/**
	 * 测试方法
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

	}

}
